
<template>
  <div 
    class="magic-cube" 
    :style="containerStyle"
  >
    <!-- 默认状态 -->
    <div v-if="!showImageList" class="empty-state">
      <el-empty description="暂无魔方内容" />
    </div>

    <!-- 一行两个 -->
    <div
      v-else-if="data.layoutType === 'two-per-row'"
      class="magic-cube-layout"
    >
      <div
        v-for="(item, index) in data.items.slice(0, 2)"
        :key="index"
        class="magic-cube-item two-per-row"
        :style="itemStyle"
      >
        <img
          :src="item.image || defaultImage"
          :alt="item.title"
          @click="handleItemClick(item)"
        />
      </div>
    </div>

    <!-- 一行三个 -->
    <div
      v-else-if="data.layoutType === 'three-per-row'"
      class="magic-cube-layout"
    >
      <div
        v-for="(item, index) in data.items.slice(0, 3)"
        :key="index"
        class="magic-cube-item three-per-row"
        :style="itemStyle"
      >
        <img
          :src="item.image || defaultImage"
          :alt="item.title"
          @click="handleItemClick(item)"
        />
      </div>
    </div>

    <!-- 一行四个 -->
    <div
      v-else-if="data.layoutType === 'four-per-row'"
      class="magic-cube-layout"
    >
      <div
        v-for="(item, index) in data.items.slice(0, 4)"
        :key="index"
        class="magic-cube-item four-per-row"
        :style="itemStyle"
      >
        <img
          :src="item.image || defaultImage"
          :alt="item.title"
          @click="handleItemClick(item)"
        />
      </div>
    </div>

    <!-- 二左二右 -->
    <div
      v-else-if="data.layoutType === 'two-left-two-right'"
      class="magic-cube-layout two-left-two-right"
    >
      <div class="left-column">
        <div
          v-for="(item, index) in data.items.slice(0, 2)"
          :key="index"
          class="magic-cube-item"
          :style="itemStyle"
        >
          <img
            :src="item.image || defaultImage"
            :alt="item.title"
            @click="handleItemClick(item)"
          />
        </div>
      </div>

      <div class="right-column">
        <div
          v-for="(item, index) in data.items.slice(2, 4)"
          :key="index"
          class="magic-cube-item"
          :style="itemStyle"
        >
          <img
            :src="item.image || defaultImage"
            :alt="item.title"
            @click="handleItemClick(item)"
          />
        </div>
      </div>
    </div>

    <!-- 一左二右 -->
    <div
      v-else-if="data.layoutType === 'one-left-two-right'"
      class="magic-cube-layout one-left-two-right"
    >
      <div class="left-column">
        <div
          v-for="(item, index) in data.items.slice(0, 1)"
          :key="index"
          class="magic-cube-item"
          :style="itemStyle"
        >
          <img
            :src="item.image || defaultImage"
            :alt="item.title"
            @click="handleItemClick(item)"
          />
        </div>
      </div>

      <div class="right-column">
        <div
          v-for="(item, index) in data.items.slice(1, 3)"
          :key="index"
          class="magic-cube-item"
          :style="itemStyle"
        >
          <img
            :src="item.image || defaultImage"
            :alt="item.title"
            @click="handleItemClick(item)"
          />
        </div>
      </div>
    </div>

    <!-- 一上二下 -->
    <div
      v-else-if="data.layoutType === 'one-up-two-down'"
      class="magic-cube-layout one-up-two-down"
    >
      <div class="top-row">
        <div
          v-for="(item, index) in data.items.slice(0, 1)"
          :key="index"
          class="magic-cube-item"
          :style="itemStyle"
        >
          <img
            :src="item.image || defaultImage"
            :alt="item.title"
            @click="handleItemClick(item)"
          />
        </div>
      </div>

      <div class="bottom-row">
        <div
          v-for="(item, index) in data.items.slice(1, 3)"
          :key="index"
          class="magic-cube-item"
          :style="itemStyle"
        >
          <img
            :src="item.image || defaultImage"
            :alt="item.title"
            @click="handleItemClick(item)"
          />
        </div>
      </div>
    </div>

    <!-- 一左三右 -->
    <div
      v-else-if="data.layoutType === 'one-left-three-right'"
      class="magic-cube-layout one-left-three-right"
    >
      <div class="left-column">
        <div
          v-for="(item, index) in data.items.slice(0, 1)"
          :key="index"
          class="magic-cube-item"
          :style="itemStyle"
        >
          <img
            :src="item.image || defaultImage"
            :alt="item.title"
            @click="handleItemClick(item)"
          />
        </div>
      </div>

      <div class="right-column">
        <div
          v-for="(item, index) in data.items.slice(1, 4)"
          :key="index"
          class="magic-cube-item"
          :style="itemStyle"
        >
          <img
            :src="item.image || defaultImage"
            :alt="item.title"
            @click="handleItemClick(item)"
          />
        </div>
      </div>
    </div>

    <!-- 删除组件 -->
    <slot name="deles" />
  </div>
</template>

<script setup>
import { computed } from 'vue'

// 定义组件属性
const props = defineProps({
  data: {
    type: Object,
    default: () => ({})
  },
  styles: {
    type: Object,
    default: () => ({})
  }
})

// 定义事件
const emit = defineEmits(['item-click'])

// 默认图片
const defaultImage = computed(() => '/src/assets/images/default-image.png')

// 是否显示图片列表
const showImageList = computed(() => {
  const items = props.data.items || []

  if (props.data.layoutType === 'two-per-row' && items.length >= 2) {
    return items[0].image && items[1].image
  }

  if ((props.data.layoutType === 'three-per-row' || 
       props.data.layoutType === 'one-left-two-right' || 
       props.data.layoutType === 'one-up-two-down') && items.length >= 3) {
    return items[0].image && items[1].image && items[2].image
  }

  if ((props.data.layoutType === 'four-per-row' || 
       props.data.layoutType === 'two-left-two-right' || 
       props.data.layoutType === 'one-left-three-right') && items.length >= 4) {
    return items[0].image && items[1].image && items[2].image && items[3].image
  }

  return false
})

// 计算容器样式
const containerStyle = computed(() => {
  return {
    padding: `0 ${props.data.pageMargin || 10}px`,
    marginTop: `${props.styles.marginTop || 0}px`,
    marginBottom: `${props.styles.marginBottom || 0}px`,
    marginLeft: `${props.styles.marginLeft || 0}px`,
    marginRight: `${props.styles.marginRight || 0}px`
  }
})

// 计算项目样式
const itemStyle = computed(() => {
  return {
    padding: `${props.data.imagePadding || 0}px`,
    borderRadius: `${props.data.borderRadius || 0}px`
  }
})

// 事件处理
function handleItemClick(item) {
  emit('item-click', item)
}
</script>

<style lang="scss" scoped>
.magic-cube {
  width: 100%;
  box-sizing: border-box;
  position: relative;

  .empty-state {
    min-height: 200px;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .magic-cube-layout {
    display: flex;
    flex-wrap: wrap;

    .magic-cube-item {
      position: relative;
      overflow: hidden;
      cursor: pointer;

      img {
        width: 100%;
        height: 100%;
        object-fit: cover;
        display: block;
      }
    }

    // 一行两个
    &.two-per-row {
      .magic-cube-item {
        width: 50%;
        height: 200px;

        &:first-child {
          padding-left: 0;
          padding-top: 0;
          padding-bottom: 0;
        }

        &:last-child {
          padding-right: 0;
          padding-top: 0;
          padding-bottom: 0;
        }
      }
    }

    // 一行三个
    &.three-per-row {
      .magic-cube-item {
        width: 33.333%;
        height: 150px;

        &:nth-child(1) {
          margin-left: 0;
          margin-top: 0;
          margin-bottom: 0;
        }

        &:nth-child(2) {
          margin-top: 0;
          margin-left: 0;
          margin-bottom: 0;
        }

        &:nth-child(3) {
          margin-top: 0;
          margin-right: 0;
          margin-bottom: 0;
        }
      }
    }

    // 一行四个
    &.four-per-row {
      .magic-cube-item {
        width: 25%;
        height: 120px;
      }
    }

    // 二左二右
    &.two-left-two-right {
      .left-column, .right-column {
        width: 50%;
        display: flex;
        flex-direction: column;
      }

      .magic-cube-item {
        width: 100%;
        height: 150px;
      }
    }

    // 一左二右
    &.one-left-two-right {
      .left-column {
        width: 50%;
      }

      .right-column {
        width: 50%;
        display: flex;
        flex-direction: column;
      }

      .magic-cube-item {
        width: 100%;
        height: 150px;
      }
    }

    // 一上二下
    &.one-up-two-down {
      flex-direction: column;

      .top-row {
        width: 100%;
      }

      .bottom-row {
        width: 100%;
        display: flex;
      }

      .magic-cube-item {
        width: 100%;
        height: 150px;
      }

      .bottom-row .magic-cube-item {
        width: 50%;
      }
    }

    // 一左三右
    &.one-left-three-right {
      .left-column {
        width: 50%;
      }

      .right-column {
        width: 50%;
        display: flex;
        flex-wrap: wrap;
      }

      .magic-cube-item {
        width: 100%;
        height: 150px;
      }

      .right-column .magic-cube-item {
        width: 50%;
        height: 75px;
      }
    }
  }
}
</style>
